## coding: UTF-8
#'''
#Created on 2011-6-10
#
#@author: yang_wanjun
#'''
#import os
#
#from models import Task
#from django.http import HttpResponse
#from django.conf import settings
#from django.utils.encoding import force_unicode
#
#from whoosh.index import create_in, open_dir
#from whoosh.fields import Schema, TEXT, ID, DATETIME
#from whoosh.qparser import QueryParser
#from whoosh.analysis import RegexAnalyzer
#
#def index_process(request):
#    ix = get_index()
#    writer = ix.writer()
#    
#    tasks_for_add = Task.objects.get_add_tasks()
#    for task in tasks_for_add:
#        writer.add_document(title=force_unicode(task.content_object.title)
#                            , path=force_unicode(task.content_object.get_absolute_url())
#                            , content=force_unicode(task.content_object.contents)
#                            , date=task.content_object.created_date)
##        task.status = TASK_STATUS_COMPLETE
##        task.save()
#        
#    writer.commit()
#    return HttpResponse(content='Success!')
#
#def search_process(keyword):
#    ix = get_index()
#    ret_list = list()
#    try:
#        searcher = ix.searcher()
#        query = QueryParser("content", ix.schema).parse(force_unicode(keyword))
#        results = searcher.search(query)
#        if len(results) > 0:
#            for hit in results:
#                ret_list.append({'title':hit['title'], 'path':hit['path']})
#        return ret_list
#    finally:
#        searcher.close()
#    
#def get_index():
#    analyzer = RegexAnalyzer(ur'([\u4e00-\u9fa5])|(\w+(\.?\w+)*)')
#    schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT(analyzer=analyzer)
#                    , date=DATETIME)
#    if not os.path.exists(settings.INDEX_ROOT_PATH):
#        os.mkdir(settings.INDEX_ROOT_PATH)
#        ix = create_in(settings.INDEX_ROOT_PATH, schema, indexname='article_indexes')
#    else:
#        ix = open_dir(settings.INDEX_ROOT_PATH, indexname='article_indexes')
#        
#    return ix
